local ls = require("luasnip")
local s = ls.snippet
local t = ls.text_node
local i = ls.insert_node
local f = ls.function_node
local fmt = require("luasnip.extras.fmt").fmt

ls.add_snippets("typescript", {
  -- TypeScript interface
  s("int", fmt([[
  interface {} {{
    {}: {}
  }}
  ]], {
    i(1, "InterfaceName"),
    i(2, "property"),
    i(3, "type"),
  })),

  -- TypeScript type
  s("typ", fmt([[
  type {} = {}
  ]], {
    i(1, "TypeName"),
    i(2, "type"),
  })),

  -- Type assertion
  s("as", fmt("{} as {}", {
    i(1, "value"),
    i(2, "type"),
  })),

  -- Enum
  s("enum", fmt([[
  enum {} {{
    {} = {},
  }}
  ]], {
    i(1, "EnumName"),
    i(2, "KEY"),
    i(3, "value"),
  })),

  -- Generic interface
  s("gint", fmt([[
  interface {}<{}> {{
    {}: {}
  }}
  ]], {
    i(1, "InterfaceName"),
    i(2, "T"),
    i(3, "property"),
    i(4, "T"),
  })),

  -- Generic type
  s("gtyp", fmt([[
  type {}<{}> = {}
  ]], {
    i(1, "TypeName"),
    i(2, "T"),
    i(3, "type"),
  })),

  -- Generic function
  s("gfn", fmt([[
  function {}<{}>({}): {} {{
    {}
  }}
  ]], {
    i(1, "functionName"),
    i(2, "T"),
    i(3, "param: T"),
    i(4, "T"),
    i(5, "return param"),
  })),

  -- Type guard
  s("is", fmt([[
  function is{}(value: unknown): value is {} {{
    {}
  }}
  ]], {
    i(1, "Type"),
    i(2, "Type"),
    i(3, "// type check"),
  })),

  -- Readonly interface
  s("roint", fmt([[
  interface {} {{
    readonly {}: {}
  }}
  ]], {
    i(1, "InterfaceName"),
    i(2, "property"),
    i(3, "type"),
  })),

  -- Union type
  s("uni", fmt([[
  type {} = {} | {}
  ]], {
    i(1, "UnionType"),
    i(2, "Type1"),
    i(3, "Type2"),
  })),

  -- Intersection type
  s("int", fmt([[
  type {} = {} & {}
  ]], {
    i(1, "IntersectionType"),
    i(2, "Type1"),
    i(3, "Type2"),
  })),

  -- Partial type
  s("par", fmt([[
  type {} = Partial<{}>]], {
    i(1, "PartialType"),
    i(2, "Type"),
  })),

  -- Pick type
  s("pick", fmt([[
  type {} = Pick<{}, {}>]], {
    i(1, "PickType"),
    i(2, "Type"),
    i(3, "keys"),
  })),

  -- Omit type
  s("omit", fmt([[
  type {} = Omit<{}, {}>]], {
    i(1, "OmitType"),
    i(2, "Type"),
    i(3, "keys"),
  })),

  -- Record type
  s("rec", fmt([[
  type {} = Record<{}, {}>]], {
    i(1, "RecordType"),
    i(2, "KeyType"),
    i(3, "ValueType"),
  })),

  -- Mapped type
  s("map", fmt([[
  type {} = {{
    [K in {}]: {}
  }}
  ]], {
    i(1, "MappedType"),
    i(2, "KeyType"),
    i(3, "ValueType"),
  })),

  -- Conditional type
  s("cond", fmt([[
  type {} = {} extends {} ? {} : {}
  ]], {
    i(1, "ConditionalType"),
    i(2, "Type"),
    i(3, "Condition"),
    i(4, "TrueType"),
    i(5, "FalseType"),
  })),

  -- Utility type: Required
  s("req", fmt([[
  type {} = Required<{}>]], {
    i(1, "RequiredType"),
    i(2, "Type"),
  })),

  -- Utility type: Readonly
  s("ro", fmt([[
  type {} = Readonly<{}>]], {
    i(1, "ReadonlyType"),
    i(2, "Type"),
  })),
})

-- 让 TypeScript 代码片段在 TSX 中可用
ls.filetype_extend("typescriptreact", { "typescript" })